package com.mediacloud.app.newsmodule.activity.audio;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.model.utils.ViewUtils;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.utils.GridSpacingItemDecoration;
import com.mediacloud.app.reslib.enums.XKey;
import com.tencent.mmkv.MMKV;
import com.utils.ClickUtils;

public class AudioAlbumProgramWinX2 extends PopupWindow {

    protected View contentView;
    protected Context mContext;
    private RecyclerView recyclerView;
    private TextView titleTv;
    private TextView tv_close;

    private View maskView;

    public AudioAlbumProgramWinX2(Context context) {
        super(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
        mContext = context;

        contentView = LayoutInflater.from(mContext).inflate(R.layout.aduio_album_select_program_list_win, null);
        setContentView(contentView);
//        contentView.setOnTouchListener((v, event) -> {
//            boolean b = contentView.onTouchEvent(event);
//            dismiss();
//            return b;
//        });
        maskView = contentView.findViewById(R.id.dark_mask);
        titleTv =contentView.findViewById(R.id.titleTv);
        tv_close=contentView.findViewById(R.id.tv_close);
        recyclerView = contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        GridSpacingItemDecoration decor = new GridSpacingItemDecoration(4, context.getResources().getDimensionPixelSize(R.dimen.dimen4), true);
        recyclerView.addItemDecoration(decor);
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.delayClickable(v);
                dismiss();
            }
        });
        setAnimationStyle(R.style.BottomDialog_Animation2);
    }

    public void setAdaptor(SelectProgramAdapter adaptor) {
        recyclerView.setAdapter(adaptor);
    }

    public void setTitleStr(String msg){
        titleTv.setText(msg);
    }


    public void show(final View view) {
        dismiss();
        int heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
        if (contentView.getHeight() <= 0) {
            contentView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    contentView.getViewTreeObserver().removeOnPreDrawListener(this);
                    dismiss();
                    int heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
                    showAtLocation(view, Gravity.NO_GRAVITY, 0, heightPixels - contentView.getHeight());
                    return true;
                }
            });
        }
        showAtLocation(view, Gravity.NO_GRAVITY, 0, heightPixels - contentView.getHeight());
    }

    public void changeDarMode(){
        if(MMKV.defaultMMKV().getBoolean(XKey.DARK_MODE,false)){
            maskView.setVisibility(View.VISIBLE);
        }else{
            maskView.setVisibility(View.GONE);
        }
    }
}